package com.platform.service;

import com.platform.dao.ApiResourceMapper;
import com.platform.dao.ApiUserMapper;
import com.platform.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ApiResourceService {
    @Autowired
    private ApiResourceMapper apiResourceMapper;
    @Autowired
    private ApiUserMapper apiUserMapper;

    public void save(ApiResourceVo resourceVo) {
        apiResourceMapper.save(resourceVo);
    }

    public void updataByUserVo(String picIds, Long mouldId, String mouldName, Integer type) {

        UserVo userVo = apiUserMapper.queryObject(mouldId);
        String redArr[] = picIds.split(",");

        for (int i = 0; i < redArr.length; i++) {

            if (redArr[i] != null && !redArr[i].isEmpty()) {
                ApiResourceVo apiResourceVo = apiResourceMapper.queryObject(Long.valueOf(redArr[i]));

                if (apiResourceVo != null) {

                    if (i == 0) {
                        userVo.setCover_url(apiResourceVo.getUrl());
                        apiUserMapper.update(userVo);
                    }
                    apiResourceVo.setMouldId(mouldId);
                    apiResourceVo.setMouldName(mouldName);
                    apiResourceVo.setType(type);
                    apiResourceMapper.update(apiResourceVo);
                }
            }
        }
    }

    public List<ApiResourceVo> selectList(Map<String, Object> params) {
        return apiResourceMapper.queryList(params);
    }

    public void del(Integer id) {
        apiResourceMapper.delete(id);
    }
}
